package com.biz.primus.ms.order.service.impl;

import com.biz.primus.base.exception.BizSilentException;
import com.biz.primus.model.oms.vo.returns.UserFillReturnExpressInfoRequestVo;
import com.biz.primus.model.order.enums.ReturnState;
import com.biz.primus.model.order.exception.OrderException;
import com.biz.primus.model.order.exception.utils.AssertRequestParameter;
import com.biz.primus.model.order.vo.aftersale.QueryOrderResponseVO;
import com.biz.primus.model.order.vo.aftersale.ReturnOrderAddLogisticsRequestVO;
import com.biz.primus.model.order.vo.aftersale.ReturnOrderRequestVO;
import com.biz.primus.model.order.vo.aftersale.ReturnOrderResponseVO;
import com.biz.primus.ms.order.convertor.OrderConvertor;
import com.biz.primus.ms.order.convertor.ReturnOrderConvertor;
import com.biz.primus.ms.order.dao.po.Order;
import com.biz.primus.ms.order.dao.po.ReturnOrder;
import com.biz.primus.ms.order.service.ReturnOrderService;
import com.biz.primus.ms.order.service.bulider.CreateReturnOrderBuilder;
import com.biz.primus.ms.order.service.helper.OrderServiceHelper;
import com.biz.primus.ms.order.service.helper.ReturnDataStreamHelper;
import com.biz.primus.ms.order.service.helper.ReturnStateStreamHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 退货订单服务实现
 *
 * @author Tiger Feng
 * @date 2017年11月20日
 */
@Slf4j
@Service
public class ReturnOrderServiceImpl implements ReturnOrderService {
    @Autowired
    private OrderServiceHelper orderServiceHelper;
    @Autowired
    private ReturnDataStreamHelper dataStreamHelper;
    @Autowired
    private ReturnStateStreamHelper stateStreamHelper;

    @Override
    public QueryOrderResponseVO getByOrderCode4AfterSale(String orderCode) {
        AssertRequestParameter.notNull(orderCode, "订单编号不能为空");
        Order order = orderServiceHelper.getByOrderCode(orderCode);
        AssertRequestParameter.notNull(order, "订单不存在");
        return OrderConvertor.order2ApplyForAfterSaleOrderResponseVO(order);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ReturnOrderResponseVO applyReturnOrder(ReturnOrderRequestVO returnOrderRequestVO) {
        CreateReturnOrderBuilder builder = CreateReturnOrderBuilder.newBuilder(returnOrderRequestVO, orderServiceHelper, dataStreamHelper);
        return builder.check().create().send2OMS().build();
    }

    @Override
    public ReturnOrderResponseVO getByOrderCode(String orderCode) {
        AssertRequestParameter.hasLength(orderCode, "订单编号不能为空");
        return ReturnOrderConvertor.po2RespVO(orderServiceHelper.getReturnOrderByOrderCode(orderCode));
    }

    @Override
    public ReturnOrderResponseVO addLogisticsInfo(ReturnOrderAddLogisticsRequestVO returnOrderAddLogisticsRequestVO) {
        AssertRequestParameter.notNull(returnOrderAddLogisticsRequestVO, "退货单物流信息请求模型不能为空");
        AssertRequestParameter.hasLength(returnOrderAddLogisticsRequestVO.getOrderCode(), "订单编码不能为空");
        ReturnOrder returnOrder = orderServiceHelper.getReturnOrderByOrderCode(returnOrderAddLogisticsRequestVO.getOrderCode());
        AssertRequestParameter.notNull(returnOrder, "退货单不存在");
        switch (returnOrder.getAuditState()) {
            case DISAGREE:
                throw new BizSilentException(OrderException.RETURN_STATE_ERROR);
            case UNAUDITED:
                throw new BizSilentException(OrderException.RETURN_STATE_ERROR);
            case AGREE:
                break;
            default:
                throw new BizSilentException(OrderException.NOT_SUPPORT_RETURN_STATE);
        }
        returnOrder.setExpressName(returnOrderAddLogisticsRequestVO.getExpressName());
        returnOrder.setExpressNumber(returnOrderAddLogisticsRequestVO.getExpressNumber());
        returnOrder.setExpressUserDescription(returnOrderAddLogisticsRequestVO.getExpressDescription());
        returnOrder.setExpressUserImages(returnOrderAddLogisticsRequestVO.getExpressUserImages());
        returnOrder = orderServiceHelper.saveReturnOrder(returnOrder);
        ReturnOrderResponseVO responseVO = ReturnOrderConvertor.po2RespVO(returnOrder);
        // 推送物流信息给OMS
        UserFillReturnExpressInfoRequestVo returnExpress = ReturnOrderConvertor.po2OmsReturnExpressDataVO(returnOrder);
        dataStreamHelper.sendExpress(returnExpress);
        return responseVO;
    }

    @Override
    public ReturnOrderResponseVO storeReceived(String orderCode) {
        AssertRequestParameter.hasLength(orderCode, "订单编码不能为空");
        ReturnOrder returnOrder = orderServiceHelper.getReturnOrderByOrderCode(orderCode);
        AssertRequestParameter.notNull(returnOrder, "退货单不存在");
        switch (returnOrder.getReturnState()) {
            case RETURNING:
                break;
            default:
                throw new BizSilentException(OrderException.RETURN_STATE_ERROR);
        }
        returnOrder.setReturnState(ReturnState.WAIT_REFUND);
        return ReturnOrderConvertor.po2RespVO(orderServiceHelper.saveReturnOrder(returnOrder));
    }

    @Override
    public ReturnOrderResponseVO storeRefund(String orderCode) {
        AssertRequestParameter.hasLength(orderCode, "订单编码不能为空");
        ReturnOrder returnOrder = orderServiceHelper.getReturnOrderByOrderCode(orderCode);
        AssertRequestParameter.notNull(returnOrder, "退货单不存在");
        switch (returnOrder.getReturnState()) {
            case WAIT_REFUND:
                break;
            default:
                throw new BizSilentException(OrderException.RETURN_STATE_ERROR);
        }
        returnOrder.setReturnState(ReturnState.REFUNDED);
        return ReturnOrderConvertor.po2RespVO(orderServiceHelper.saveReturnOrder(returnOrder));
    }

    @Override
    public Boolean cancelReturnOrder(String returnCode) {
        AssertRequestParameter.hasLength(returnCode, "退货单编码不能为空");
        ReturnOrder returnOrder = orderServiceHelper.getreturnOrderByReturnCode(returnCode);
        AssertRequestParameter.notNull(returnOrder, "退货单不存在");
        switch (returnOrder.getReturnState()) {
            case CANCELED:
                // 已取消，不用重复取消
                return true;
            case REFUNDED:
                // 已退款,不能取消
                throw new BizSilentException(OrderException.CANNOT_CANCEL_REFUNDED);
            default:
                break;
        }
        returnOrder.setReturnState(ReturnState.CANCELED);
        returnOrder = orderServiceHelper.saveReturnOrder(returnOrder);
        if (returnOrder != null && returnOrder.getReturnState() == ReturnState.CANCELED) {
            // 通知OMS，退货单取消
            stateStreamHelper.sendCancelReturnOrder(returnOrder);
            return true;
        }
        return false;
    }
}
